<?php

header ('Content-type: text/html; charset=UTF-8'); 


?>


<script src="system.js"></script>
<script src="md5-min.js"></script>
<script src="hmac-md5.js"></script>


<script language="javascript" type="text/javascript">




function returnExecuteTransformationAllItens(jsonObj){
	
	
	if( jsonObj.status == 1){
	
		alert( jsonObj.data );
		window.location.href='view_list.php';
		
	}else{
		
		alert( jsonObj.data );
	}
	
	
	//document.getElementById("loading").style="visibility:hidden";
}
		

function executeTransformationAllItens(view_id) 
{
	
	if( view_id ==="null" 
		|| view_id===null 
		|| view_id==="" 
		|| typeof view_id === "undefined"  ){
			
			alert("Selecione uma visão.");
			
	}else{
		
		
		var r = confirm("Confirma execução de toda as regras de transformação?");
		if (r == true)
		{
			
			var oObject = {} ;
							
			oObject["view_id"] = view_id;
			oObject["component"] = "etl";
			oObject["controller"] = "transformation";
			oObject["task"] = "executeAllItens";
			
			
			var jsonVales	=	convertToJSON(oObject);
			
			parameters	=	encodeURIComponent(
									convertToStringJSON(jsonVales));
			
			var method='PUT';
			
			httpRequest ( method,
							url_API,
							parameters,
							'returnExecuteTransformationAllItens');
	
			document.getElementById("loading").style="visibility:visible";
		}
		
		
	}
	
	
}



function returnCloneView(jsonObj){

	alert( jsonObj.data );
	
	if( jsonObj.status == 1){
	
		window.location.href='view_list.php';

	}
	
}


function cloneView(id){
	
	var r = confirm("Confirma clonar view?");
	if (r == true)
	{
		var oObject = {} ;
						
		oObject["id"] = id;
		oObject["component"] = "etl";
		oObject["controller"] = "view";
		oObject["task"] = "clone";
		
		
		var jsonVales	=	convertToJSON(oObject);
		
		parameters	=	encodeURIComponent(
								convertToStringJSON(jsonVales));
		
		var method='POST';
			
		httpRequest ( method,
						url_API,
						parameters,
						'returnCloneView');
						
	}
	
}


function returnListviewData(jsonObj){
	
	
	
	if( jsonObj.status == 1){
		

		var grid_view_html="";
		

		var obj_json = JSON.parse(jsonObj.data);

		var i;
		//alert(obj_json.length);
		var columns="";
		
		
		for (i=0; i < obj_json.length; i++){
		    //var img = new Image();
		    //img.setAttribute("src",obj[i][2] + obj[i][1]);
		    //document.body.appendChild(img);
		    //alert( obj_json[i].name_view );
		    
		    //alert("name="+obj_json[i].name );
		    
		    if( columns==''){
		    	columns = columns +"<tr>";
		    	columns = columns +"<td>#</td>";
		    	//columns = columns +"<td>acesso</td>";
		    	
		    	for (var key in obj_json[i]) {
			    	
					columns = columns +"<td>"+key+"</td>";
			    	
				}
				columns = columns +"</tr>";
		    }
		    
			/*
			grid_view_html = grid_view_html +"<tr>";
		    grid_view_html = grid_view_html	+"<td style='width:200px;'>"
		    				+"<input type=\"checkbox\" name=\"id[]\" value=\""+obj_json[i].id+"\" />"
		    				+"<a href='view_edit.php?id="+obj_json[i].id+"'>Editar</a><br>"
		    				+"|| <a href='javascript:cloneView("+obj_json[i].id+");'>Clonar</a><br>"
		    				+"|| <a href='javascript:viewExecute("+obj_json[i].id+")'>Executar (Extração)</a><br>"
		    				+"|| <a href='javascript:deleteItem("+obj_json[i].id+");'>Excluir</a><br>"
		    				+"|| <a target='_blank' href='view_data.php?id="+obj_json[i].id+"'>Dados</a><br>"
		    				+"|| <a href='http://127.0.0.1:8080/api/apiopengovernmentdata/?key="+obj_json[i].table_name+"&download=1&notation=csv'>Download csv</a><br>"
		    				+"|| <a href='http://127.0.0.1:8080/api/apiopengovernmentdata/?key="+obj_json[i].table_name+"&download=1&notation=json'>Downloas json</a><br>"
		    				+"|| <a href='http://127.0.0.1:8080/api/apiopengovernmentdata/?key="+obj_json[i].table_name+"&download=1&notation=csv&metadata=1'>Download metadata csv</a><br>"
		    				+"|| <a href='http://127.0.0.1:8080/api/apiopengovernmentdata/?key="+obj_json[i].table_name+"&download=1&notation=json&metadata=1'>Download metadata json</a><br>"
		    				+"|| <a href='view_metadata_edit.php?id="+obj_json[i].id+"'>metadata edit</a><br>"
		    				//+"|| <a href=\"javascript: getAccess('"+obj_json[i].table_name+"','access','','');\">Acesso (total)</a><br>"
		    				//+"|| <a href=\"javascript: getAccess('"+obj_json[i].table_name+"','accessDownloadAll','','');\">Acesso Download(total)</a><br>"
		    				//+"|| <a href=\"javascript: getAccess('"+obj_json[i].table_name+"','accessDownloadNotation','1','csv');\">Acesso Download (csv)</a><br>"
		    				//+"|| <a href=\"javascript: getAccess('"+obj_json[i].table_name+"','accessDownloadNotation','1','json');\">Acesso Download (json)</a><br>"
		    				//+"|| <a href=\"javascript: getAccess('"+obj_json[i].table_name+"','accessMetadataAll','0','');\">Acesso Metadata (total)</a><br>"
		    				//+"|| <a href=\"javascript: getAccess('"+obj_json[i].table_name+"','accessMetadataNotation','0','csv');\">Acesso Metadata (csv)</a><br>"
		    				//+"|| <a href=\"javascript: getAccess('"+obj_json[i].table_name+"','accessMetadataNotation','0','json');\">Acesso Metatada (json)</a><br>"
		    				+"|| <a href='view_statistics_download.php?id="+obj_json[i].id+"'>Estatística de Acesso</a><br>"
		    				+"|| <a href='javascript:executeTransformationAllItens("+obj_json[i].id+")'>Executar (Transformação)</a><br>"
		    				+"|| <a href=\"javascript:window.location.href='transformation_list.php?view_id="+obj_json[i].id+"';\">Regras de Transformação</a></td>"
		    				+"";
		    				*/
		    					
		    grid_view_html = grid_view_html +"<tr onclick=\"javascript:document.getElementById('id"+obj_json[i].id+"').checked=true;\">";
		    grid_view_html = grid_view_html	+"<td style='width:20px;'>"
		    				+"<input type=\"radio\" name=\"id\" id='id"+obj_json[i].id+"' value=\""+obj_json[i].id+"\" />"
		    				+"</td>"
		    				+"";
		    		    
		    		    
		    		    //getAccess("c20ad4d76fe97759aa27a0c99bff6710","","");
		    		   
		    		    
		    		    
			
		    for (var key in obj_json[i]) {
			    	
				grid_view_html = grid_view_html +"<td>"+obj_json[i][key]+"</td>";
			    	
			}

		    grid_view_html = grid_view_html +"</tr>";
		    	
		    
		    // grid_view_html = grid_view_html +"<tr>"
// 		    		
		    			// +"<td>"+obj_json[i].id+"</td>"
		    			// +"<td>"+obj_json[i].name_view+"</td>"
		    			// +"<td>"+obj_json[i].table_name+"</td>"
		    			// +"<td>"+obj_json[i].db_hostip+"</td>"
		    			// +"<td>"+obj_json[i].detail+"</td>"
		    			// +"<td>"+obj_json[i].date_create+"</td>"
		    			// +"</tr>";
		    
		}


		document.getElementById("grid_view").innerHTML = "<table border=1>"+columns+grid_view_html+"</table>";
		//document.getElementById("grid_view").innerHTML = "<table border=1><tr><td>#</td><td>id</td><td>nome</td><td>table</td><td>host</td><td>detalhe</td><td>criado</td></tr>"+grid_view_html+"</table>";

		
		
	}else{
		
		alert(jsonObj.data);
		
		if(jsonObj.notification==25){
			window.location.href='index.php';
		}
	}


	
}


function getList(limit, offset, column, order) 
{
	
	var oObject = {} ;
					
	oObject["id"] = 0;
	oObject["limit"] = limit;
	oObject["offset"] = offset;
	oObject["column"] = column;
	oObject["order"] = order;
	oObject["component"] = "etl";
	oObject["controller"] = "view";
	oObject["task"] = "getList";
	
	
	var jsonVales	=	convertToJSON(oObject);
	
	parameters	=	encodeURIComponent(
							convertToStringJSON(jsonVales));
	
	var method='GET';
		
	httpRequest ( method,
					url_API,
					parameters,
					'returnListviewData');
}



function deleteItem(id) 
{
	
	var r = confirm("Confirma excluir view?");
	if (r == true)
	{
	
		var oObject = {} ;
		
		oObject["id"] = id;
		oObject["component"] = "etl";
		oObject["controller"] = "view";
		oObject["task"] = "delete";
		
		
		var jsonVales	=	convertToJSON(oObject);
		
		parameters	=	encodeURIComponent(
								convertToStringJSON(jsonVales));
		
		var method='DELETE';
			
			
		httpRequest ( method,
						url_API,
						parameters,
						'retornDelete');
						
	}

}

function retornDelete(jsonObj){
	
	if( jsonObj.status == 1){
		
		window.location.href='view_list.php';
			
	}else{
		
		alert( jsonObj.data );
	}
}



function returnListviewDataExecute(jsonObj){
	
	
	//if( jsonObj.status == 1){
		
	//	alert( jsonObj.data );
			
	//}else{
		alert( jsonObj.data );
		window.location.href='view_list.php';
	//}
	
	
	document.getElementById("loading").style="visibility:hidden";
}
		

function viewExecute(id) 
{
	
	var r = confirm("Confirma executar view?");
	if (r == true)
	{
		
		var oObject = {} ;
						
		oObject["id"] = id;
		oObject["component"] = "etl";
		oObject["controller"] = "extraction";
		oObject["task"] = "execute";
		
		
		var jsonVales	=	convertToJSON(oObject);
		
		parameters	=	encodeURIComponent(
								convertToStringJSON(jsonVales));
		
		var method='POST';
			
		httpRequest ( method,
						url_API,
						parameters,
						'returnListviewDataExecute');

		document.getElementById("loading").style="visibility:visible";
	}
	
}



function getAccess(key,task,metadata,notation) 
{
	
	var oObject = {} ;
					
	oObject["key"] = key;
	oObject["metadata"] = metadata;
	oObject["notation"] = notation;
	oObject["component"] = "statistics";
	oObject["controller"] = "download";
	oObject["task"] = task;

	
	
	var jsonVales	=	convertToJSON(oObject);
	
	parameters	=	encodeURIComponent(
							convertToStringJSON(jsonVales));
	
	var method='GET';
		
	httpRequest ( method,
					url_API,
					parameters,
					'returnAccess');
}



function returnAccess(jsonObj){

	
	var obj_json = JSON.parse(jsonObj.data);
	
	document.getElementById(obj_json[0].key).innerHTML=obj_json[0].access;
	
}


function getItemCheckedRadioId(idElement){

    var elements = document.getElementsByName(idElement);//document.getElementsByName(idElement);

    for (var i=0, len=elements.length; i<len; ++i)
        if (elements[i].checked) return elements[i].value;


}

</script>


<h1>Lista de Visões</h1>

<div id="text-result-view" style="visibility:hidden;width:1px;height:1px;"></div>
<div id="text-result-hidden" style="visibility:hidden;width:1px;height:1px;"></div>
<div id="text-result-hidden-username" style="visibility:hidden;width:1px;height:1px;"></div>

<div id="text-result">Area restria aos usuarios autenticados. voce esta logado como <div id="username-id"></div>.<a href="#" onclick="javascript:logout();">Clique aqui para Logout</a></div>
 
<br>


<a href="home_authenticated.php">Página inicial</a><br>

<a href="view_edit.php">Nova Visão</a><br>

<table>
	<tr>
		<td>
			Visão
		</td>
		<td>
			
			<input type="button" onclick="javascript:window.location.href='view_data.php?id='+getItemCheckedRadioId('id');" value="Visualizar Dados" name="Dados"/>

			<input type="button" onclick="javascript:window.location.href='view_edit.php?id='+getItemCheckedRadioId('id')" value="Editar" name="editar"/>

			<input type="button" onclick="javascript:cloneView(+getItemCheckedRadioId('id'));" value="Clonar" name="clonar"/>
			<input type="button" onclick="javascript:deleteItem(+getItemCheckedRadioId('id'));" value="Excluir" name="excluir"/>
			
		</td>
	</tr>
	<tr>
		<td>
			Extração
		</td>
		<td>
			
			<input type="button" onclick="javascript:viewExecute(+getItemCheckedRadioId('id'));" value="Executar Extração" name="executar"/>

		</td>
	</tr>
	<tr>
		<td>
			Download
		</td>
		<td>
		
<input type="button" onclick="javascript:var person=prompt('URL para download','http://127.0.0.1:8080/api/apiopengovernmentdata/key/'+hex_md5(getItemCheckedRadioId('id'))+'/download/1/notation/csv');window.location.href='http://127.0.0.1:8080/api/apiopengovernmentdata/key/'+hex_md5(getItemCheckedRadioId('id'))+'/download/1/notation/csv';" value="Download csv" name="Downloadcsv"/>
<input type="button" onclick="javascript:var person=prompt('URL para download','http://127.0.0.1:8080/api/apiopengovernmentdata/key/'+hex_md5(getItemCheckedRadioId('id'))+'/download=1/notation/json');window.location.href='http://127.0.0.1:8080/api/apiopengovernmentdata/key/'+hex_md5(getItemCheckedRadioId('id'))+'/download/1/notation/json';" value="Downloas json" name="Dadods"/>
<input type="button" onclick="javascript:var person=prompt('URL para download','http://127.0.0.1:8080/api/apiopengovernmentdata/key/'+hex_md5(getItemCheckedRadioId('id'))+'/download=1/notation/csv/metadata/1'); window.location.href='http://127.0.0.1:8080/api/apiopengovernmentdata/key/'+hex_md5(getItemCheckedRadioId('id'))+'/download/1/notation/csv/metadata/1';" value="Download metadata csv" name="Daddos"/>
<input type="button" onclick="javascript:var person=prompt('URL para download','http://127.0.0.1:8080/api/apiopengovernmentdata/key/'+hex_md5(getItemCheckedRadioId('id'))+'/download=1/notation/json/metadata/1'); window.location.href='http://127.0.0.1:8080/api/apiopengovernmentdata/key/'+hex_md5(getItemCheckedRadioId('id'))+'/download/1/notation/json/metadata/1';" value="Download metadata json" name="Dadods"/>

		</td>
	</tr>
	<tr>
		<td>
			Metadata
		</td>
		<td>
			
			<input type="button" onclick="javascript:window.location.href='view_metadata_edit.php?id='+getItemCheckedRadioId('id');" value="Metadata edit" name="Dadoffs"/>

		</td>
	</tr>
	<tr>
		<td>
			Estatística
		</td>
		<td>
			
			<input type="button" onclick="javascript:window.location.href='view_statistics_download.php?id='+getItemCheckedRadioId('id');" value="Estatística de Acesso" name="Dadffdoffs"/>

	
		</td>
	</tr>
	<tr>
		<td>
			Transformação
		</td>
		<td>
			
			<input type="button" onclick="javascript:window.location.href='transformation_list.php?view_id='+getItemCheckedRadioId('id');" value="Visualizar Regras de Transformação" name="sdsdfsdfs"/>
			<input type="button" onclick="javascript:executeTransformationAllItens(getItemCheckedRadioId('id'));" value="Executar (Transformação)" name="exsdfecutar"/>

		</td>
	</tr>
</table>











	    			


		    		
		    				
		    		
	
<div id="loading" style="visibility: hidden;"><img src="loading.gif"></div>
<div id="grid_view"></div>




    
<script>
getUsername();
getList(10,0,"id","desc");

</script>